//==============================================================================
// Routines to return display lines   
//==============================================================================  
#include "MP.h"

static char line1[MAX_STRING];
static char line2[MAX_STRING];
static char rec_line1[MAX_STRING];
static char rec_line2[MAX_STRING];

//================================================================================================================================
// checks the volume display counter running 
// returns 0 if clock not running else 1
//================================================================================================================================
int __stdcall MP_check_vol_counter()
{ double tdiff,end_time; int counter;

  if (MP_Global_prev>0)
    { end_time=(double)clock()/CLOCKS_PER_SEC;       // Counter time
      tdiff=end_time-MP_Global_prev_start_time;      // in seconds
      if ((int)tdiff>VOL_DISPLAY_TIME) { MP_Global_prev=0; return 0; }
      return 1;
    }
  return 0;
}


//==============================================================================
// return line 1 values including translate if required for f1,f2
// Note: don't use two gtag's in the same sprintf!
//==============================================================================
extern char *MP_display_first() 
{ char str[MAX_STRING]; int i;

  // Default line 2 to position/duration times or bars
  Ttime_fmt(line2,"%d:%02d:%02d/%d:%02d:%02d",MP_itag("Position"),MP_itag("Duration"));

  if (!strncmp(MP_gtag("IsPaused"),"true",4))  
     { strcpy(line2,"Pause"); }
  else if (!strncmp(MP_gtag("IsPlaying"),"true",4)) 
    {
      if (!strncmp(MP_gtag("IsTv"),"true",4)) 
        { strcpy(str,MP_gtag("Properties.TV.View.channel"));
          if (!strncmp(&str[strlen(str)-13],"(Recorded TV)",13))
            { strcpy(line1,str); strcat(line1," - "); 
              strcat(line1,MP_gtag("Properties.TV.View.title"));
            }
          else
            { strcpy(line1,"TV-"); 
              strcpy(str,MP_gtag("Properties.TV.View.channel"));
              if (strlen(str)==0) strcpy(str,MP_gtag("Title"));
              strcat(line1,str);
              strcpy(line2,MP_gtag("Properties.TV.View.title")); }
            }
      else if (!strncmp(MP_gtag("CurrentModule"),"Music",5))
        { strcpy(line1,MP_gtag("MediaInfo.Artist")); 
          if(strlen(line1)>0) strcat(line1," - ");
          strcat(line1,MP_gtag("MediaInfo.Title")); 
          if (strlen(line1)==0) strcpy(line1,MP_gtag("Title"));
        }
          
      else if (!strncmp(MP_gtag("CurrentModule"),"My TV",5))
        { strcpy(line1,MP_gtag("Title")); }

      else if (!strncmp(MP_gtag("CurrentModule"),"Moving Pictures",15))
        { strcpy(line1,MP_gtag("MediaInfo.Title"));
          if (strlen(line1)==0) strcpy(line1,MP_gtag("MediaInfo.Title"));
          if (strlen(line1)==0) strcpy(line1,MP_gtag("Properties.Play.Current.Title"));  }

      else if (!strncmp(MP_gtag("CurrentModule"),"Fullscreen video",16))
        { strcpy(line1,MP_gtag("Title")); 
          if (strlen(line1)==0) strcpy(line1,MP_gtag("MediaInfo.Title"));
          if (strlen(line1)==0) strcpy(line1,MP_gtag("Properties.Play.Current.Title"));  }

      else if (!strncmp(MP_gtag("CurrentModule"),"My Last.fm radio",16))
        { strcpy(line1,"My Last.fm radio"); strcpy(line2,MP_gtag("Title")); 
          Ttime_fmt(line2,"%d:%02d:%02d/%d:%02d:%02d",MP_itag("Position"),MP_itag("Duration"));
        }

      else if (!strncmp(MP_gtag("CurrentModule"),"Online Video",12))
        { strcpy(line1,"Online Video: "); strcat(line1,MP_gtag("SelectedItem"));
          Ttime_fmt(line2,"%d:%02d:%02d/%d:%02d:%02d",MP_itag("Position"),MP_itag("Duration"));
        }

      else if (!strncmp(MP_gtag("CurrentModule"),"My Radio",8))
        { strcpy(line1,"Radio:"); strcat(line1,MP_gtag("Title")); }

      else if (strlen(MP_gtag("Properties.Play.Current.Artist"))==0)       // Playing but not TV / movie or music
        { strcpy(line1,MP_gtag("Properties.Play.Current.Title")); }

      else 
        { strcpy(line1,MP_gtag("Properties.Play.Current.Artist"));
          strcat(line1," - ");
          strcat(line1,MP_gtag("Title")); }
//          strcat(line1,MP_gtag("Properties.Play.Current.Title")); }
    }
  // Not playing and Not Paused - so output Menu info
  else if (!strncmp(MP_gtag("SelectedItem"),"dummy",5))
        { strcpy(line1,"BBC Weather"); strcpy(line2,MP_time_string()); }
  else
    { strcpy(line1,MP_gtag("CurrentModule"));
      strcpy(str,MP_gtag("SelectedItem"));
      if (strlen(str)!=0) strcpy(line2,str); // else strcpy(line2,MP_gtag("Title"));
      if ((!strncmp(line2,"Yes",3)) || (!strncmp(line2,"#homeitem",9))) strcpy(line2,MP_time_string());
      if (strlen(line1)==0) { strcpy(line1,line2); strcpy(line2,MP_time_string()); }
    }

  if (!strncmp(MP_gtag("IsMuted"),"true",4)) strcpy(line2,"Muted");
  else { if (MP_check_vol_counter()!=0) strcpy(line2,MP_Global_prev_line1); }

  if  (strlen(line1)==0) strcpy(line1,"MediaPortal"); 
  if  (strlen(line2)==0) strcpy(line2,MP_time_string());  
   
  // Check the connections are okay and if not override the output with a message
  i=0; 
  if (MP_Global_connected[WIFIREMOTE]==0) { strcpy(line1,"[WifiRemote not active]"); strcpy(line2,"---"); i=1; }
  if (MP_Global_connected[MPEXTENDED]==0) { strcpy(line2,"[MPExtended not active]"); if (i==0) strcpy(line1,"---"); }

  // Do any translations required
  MP_xlate_find(line1,line1); 
  MP_xlate_find(line2,line2);
  
  return line1; 
}

//==============================================================================
// return line 1 values including translate if required
// don't use two gtag's in the same sprintf!
//==============================================================================
extern char *MP_display_first_rec() 
{ char str[MAX_STRING];

  strcpy(rec_line1,MP_display_first());
  strcpy(rec_line2,MP_display_second());
  //
  // Create a recording line in the form :
  //            Record[n] channel_name
  //            Recording_name
  if (!strncmp(MP_gtag("IsRecording"),"true",4))
    { sprintf(rec_line1,"Record:[%d] %s",MP_itag("IRecordings"),MP_gtag("Recording1.ChannelName"));
      strcpy(rec_line2,MP_gtag("Recording1.Title")); 
    }
  return rec_line1;
}
//==============================================================================
// return line 2 values including translate if required
//==============================================================================
extern char *MP_display_second() {  return line2; }
//==============================================================================
// return line 2 values including translate if required overriden if recording
//==============================================================================
extern char *MP_display_second_rec() {  return rec_line2; }
  
//==============================================================================
// Check any counters
//==============================================================================
extern void __stdcall MP_check_timers(int initialise) 
{ double tdiff,cur_time; char str[256];

  // Check if time for a dump
  cur_time=(double)clock()/CLOCKS_PER_SEC;                // Counter time
  tdiff=cur_time-MP_Global_last_dump;                     // in seconds

  if ( ( (initialise==1) || ((int)tdiff>MP_Global_dumpinterval) ) )
    { MP_Dump(); MP_Global_last_dump=cur_time; }

  // Check if time to send a request message
  tdiff=cur_time-MP_Global_last_request;                     // in seconds
  if ( ( (initialise==1) || ((int)tdiff>REQUEST_INTERVAL) ) )
    {  if(MP_Global_authorised==1)                            // avoid sending too early
        { MP_Send(WIFIREMOTE,"{\"Type\":\"requeststatus\"}\r\n\r\n");
          MP_Send(WIFIREMOTE,"{\"Type\":\"requestnowplaying\"}\r\n\r\n");
        }
       MP_Global_last_request=cur_time; 
    }
}

